package symptomsSearch;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

import ctm.javacode.dbBean;

public class PossibleSymptoms {

	private List<List<String>> SymptomsKeywordList = new ArrayList<List<String>>();
	private List<List<String>> atrribue_not_exist_in_object_list = new ArrayList<List<String>>();

	private void PossibleSymptomsList(List<List<String>> AttributeAndValueList,
			List<List<String>> ObgectAndAttributeList) {

		for (int value_idx = 0; value_idx < AttributeAndValueList.size(); value_idx++) {

			for (int Obgect_idx = 0; Obgect_idx < ObgectAndAttributeList.size(); Obgect_idx++) {

				List<String> SymptomsKeywordData = new ArrayList<String>();

				if (ObgectAndAttributeList.get(Obgect_idx).contains(
						AttributeAndValueList.get(value_idx).get(0))) {

					SymptomsKeywordData.add(ObgectAndAttributeList.get(
							Obgect_idx).get(0));
					SymptomsKeywordData.add(AttributeAndValueList
							.get(value_idx).get(0));
					SymptomsKeywordData.add(AttributeAndValueList
							.get(value_idx).get(1));
					SymptomsKeywordList.add(SymptomsKeywordData);
				}

				else {

					List<String> atrribue_not_exist_in_object_data = new ArrayList<String>();

					atrribue_not_exist_in_object_data
							.add(ObgectAndAttributeList.get(Obgect_idx).get(0));
					atrribue_not_exist_in_object_data.add(AttributeAndValueList
							.get(value_idx).get(0));

					atrribue_not_exist_in_object_list
							.add(atrribue_not_exist_in_object_data);
				}

			}

		}

	}

	public JSONArray PossibleSymptomsQuery(
			List<List<String>> AttributeAndValueList,
			List<List<String>> ObgectAndAttributeList) throws NamingException,
			SQLException, JSONException {

		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();
		int count = 0;
		PossibleSymptomsList(AttributeAndValueList, ObgectAndAttributeList);
		JSONArray modified_Data_list = new JSONArray();

		for (int SymptomsList_idx = 0; SymptomsList_idx < SymptomsKeywordList
				.size(); SymptomsList_idx++) {

			String keyword = "%";

			for (int keyword_idx = 0; keyword_idx < SymptomsKeywordList.get(
					SymptomsList_idx).size(); keyword_idx++) {

				keyword = keyword + ":";
				keyword = keyword
						+ SymptomsKeywordList.get(SymptomsList_idx).get(
								keyword_idx) + ":%";

			}

			String sqlQuery3 = "Select * from text_to_standard_table where (standard LIKE '"
					+ keyword + "')";
			ResultSet rs3;
			rs3 = db.ExcuteQuery(sqlQuery3);

			if (rs3.next()) {

				do {

					JSONObject modified_Data = new JSONObject();
					modified_Data.put("modified", rs3.getString("text"));
					modified_Data_list.put(modified_Data);

				} while (rs3.next());

				count++;
			}

		}

		if (count == 0) {

			JSONObject modified_Data = new JSONObject();
			modified_Data.put("modified", "不存在資料庫");
			modified_Data_list.put(modified_Data);

		}
		db.CloseConn();
		return modified_Data_list;

	}

}
